/**
 * Created by Administrator on 2017/5/6.
 */
"use strict";
swan_pc
    .controller("InvestmentPerformanceController",function($scope,Cause,$rootScope,investment,orgContact,orgEvent,orgEmployee){
        $scope.width7=((document.body.clientWidth-60)/12*10-35)/7;

        $scope.performanceMode = 1;
        $scope.changePerformanceMode = function(option){
            if(option==2){
                swal("Just Waiting");
                return;
            }
            $scope.performanceMode = option;
        };

        $scope.limitMode = 1;
        $scope.changeLimitMode = function(option){
            $scope.limitMode = option;
        };

        $scope.selectCause = null;
        $scope.getCause = function(){
            $scope.causeList = [];
            Cause.query(function(result){
                $scope.causeList = result;
            })
        };
        $scope.getCause();

        $scope.loadList = function(){
            $scope.investmentOppList = [];
            $scope.events = [];
            $scope.orgContactList = [];
            investment.query({id:$rootScope.presentAccountInfo.id},function(result){
                _.forEach(result,function(data){
                    if(data.status>=6){
                        $scope.investmentOppList.push(data);
                    }
                });
            });
            orgEvent.query({id:$rootScope.presentAccountInfo.organizationId},function(result){
                $scope.events = result;
            });
            orgContact.query({page: 0, size: 1000, sort: 'asc',id:$rootScope.presentAccountInfo.id},function (result) {
                $scope.orgContactList = result;
            });
            orgEmployee.query({id:$rootScope.presentAccountInfo.id},function(result){
                $scope.employeeList = result;
            })
        };
        $scope.loadList();

        $scope.initChart = function(){
            $scope.performanceData = [];
            if($scope.selectCause == null){
                swal("请选择分析事由");
                return;
            }else{
                _.forEach($scope.investmentOppList,function(data){
                    if(data.causeId == $scope.selectCause){
                        $scope.performanceData.push(data);
                    }
                });
                switch($scope.limitMode){
                    case 1:
                        $scope.generateData1();
                        $scope.generateGoogleChart1();
                        break;
                    case 2:
                        $scope.generateData2();
                        $scope.generateGoogleChart2();
                        break;
                    case 3:
                        $scope.generateData3();
                        $scope.generateGoogleChart3();
                        break;
                    case 4:
                        $scope.generateData4();
                        $scope.generateGoogleChart4();
                        break;
                    case 5:
                        $scope.generateData4();
                        $scope.generateGoogleChart5();
                        break;

                }
            }
        };

        $scope.generateGoogleChart5 = function(){
            _.forEach($scope.performanceData,function(data){
                if(data.origin == 10){
                    _.forEach($scope.eventData,function(item){
                        if(data.eventId == item.id){
                            item.amount += data.price;
                        }
                    })
                }
            });
            $scope.drawChart5();
        };

        $scope.generateGoogleChart4 = function(){
            _.forEach($scope.performanceData,function(data){
                if(data.origin == 9){
                    _.forEach($scope.eventData,function(item){
                        if(data.eventId == item.id){
                            item.amount += data.price;
                        }
                    })
                }
            });
            $scope.drawChart4();
        };

        $scope.generateGoogleChart3 = function(){
            _.forEach($scope.performanceData,function(data){
                if(data.origin == 8){
                    _.forEach($scope.employeeData,function(item){
                        if(data.orgContactId == item.id){
                            item.amount += data.price;
                        }
                    })
                }
            });
            $scope.drawChart3();
        };
        $scope.generateGoogleChart2 = function(){
            _.forEach($scope.performanceData,function(data){
                if(data.origin == 4){
                    _.forEach($scope.contactData,function(item){
                        if(data.orgContactId == item.id){
                            item.amount += data.price;
                        }
                    })
                }
            });
            $scope.drawChart2();
        };
        $scope.generateGoogleChart1 = function(){
            _.forEach($scope.performanceData,function(data){
                _.forEach($scope.originData,function(item){
                    if(data.origin == item.id){
                        item.amount += data.price;
                    }
                })
            });
            $scope.drawChart1();
        };


        $scope.generateData4 = function(){
            $scope.eventData = [];
            _.forEach($scope.events,function(data){
                $scope.eventData.push({
                    name:data.name,
                    id:data.id,
                    amount:0
                })
            })
        };
        $scope.generateData3 = function(){
            $scope.employeeData = [];
            _.forEach($scope.employeeList,function(data){
                $scope.employeeData.push({
                    name:data.name,
                    id:data.id,
                    amount:0
                })
            })
        };
        $scope.generateData2 = function(){
            $scope.contactData = [];
            _.forEach($scope.orgContactList,function(data){
                $scope.contactData.push({
                    name:data.baseInfo.name,
                    id:data.id,
                    amount:0
                })
            })
        };
        $scope.generateData1 = function(){
            $scope.originData = [];
            _.forEach($scope.originList,function(data){
                $scope.originData.push({
                    name:data.name,
                    id:data.value,
                    amount:0
                })
            });
        };

        $scope.resetOriginData = function(){
            for(var i=0;i<$scope.originData.length;i++){
                if($scope.originData[i].amount==0){
                    $scope.originData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetContactData = function(){
            for(var i=0;i<$scope.contactData.length;i++){
                if($scope.contactData[i].amount==0){
                    $scope.contactData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetEmployeeData = function(){
            for(var i=0;i<$scope.employeeData.length;i++){
                if($scope.employeeData[i].amount==0){
                    $scope.employeeData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetEventData = function(){
            for(var i=0;i<$scope.eventData.length;i++){
                if($scope.eventData[i].amount==0){
                    $scope.eventData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.drawChart5 = function(){
            $scope.resetEventData();
            console.log($scope.eventData);
            var allItem = [["活动","所占份额"]];
            _.forEach($scope.eventData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:"招商机会市场活动来源分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart4 = function(){
            $scope.resetEventData();
            console.log($scope.eventData);
            var allItem = [["活动","所占份额"]];
            _.forEach($scope.eventData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:"招商机会推广活动来源分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart3 = function(){
            $scope.resetEmployeeData();
            console.log($scope.employeeData);
            var allItem = [["员工","所占份额"]];
            _.forEach($scope.employeeData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:"招商机会员工推荐来源分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart2 = function(){
            $scope.resetContactData();
            console.log($scope.contactData);
            var allItem = [["人员","所占份额"]];
            _.forEach($scope.contactData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:"招商机会R&R来源分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart1 = function(){
            $scope.resetOriginData();
            console.log($scope.originData);
            var allItem = [["来源分类","所占份额"]];
            _.forEach($scope.originData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:"招商机会整体来源分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };

        $scope.originList=[
            {
                name:'互联网广告',
                value:1
            },
            {
                name:'官网',
                value:2
            },
            {
                name:'800官方电话',
                value:3
            },
            {
                name:'R&R',
                value:4
            },
            {
                name:'传统广告',
                value:5
            },
            {
                name:'合作伙伴',
                value:6
            },
            {
                name:'战略客户',
                value:7
            },
            {
                name:'员工推荐',
                value:8
            },
            {
                name:'推广活动',
                value:9
            },
            {
                name:'市场活动',
                value:10
            },
            {
                name:'walk in',
                value:11
            },
            {
                name:'Coverage',
                value:12
            },
        ];



    });